package com.hp.bon.sgw.wh.utils;

import java.util.ArrayList;
import java.util.List;

import ocs.soap.AccountQueryRequest;
import ocs.soap.AccountQueryResponse;
import ocs.soap.Bonus;
import ocs.soap.RechargeRequest;
import ocs.soap.RechargeResponse;
import ocs.soap.RefundRequest;
import ocs.soap.RefundResponse;
import ocs.soap.TeleSaleAuthRequest;
import ocs.soap.TeleSaleAuthResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class SelfServiceHelper {
	private static final Logger logger = LoggerFactory
			.getLogger(SelfServiceHelper.class);

	@SuppressWarnings("unchecked")
	public static RechargeRequest convertXMLToRechargeRequest(
			Element requestElement) {
		RechargeRequest rechargeRequest = new RechargeRequest();
		org.dom4j.Node node = null;
		// Req_Serial
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Req_Serial");
		if (null != node) {
			rechargeRequest.setRequestId(node.getStringValue());
		}
		// CARD_NO
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Voucher_Id");
		if (null != node) {
			try {
				rechargeRequest.setVoucherId(node.getStringValue().trim());
			} catch (Exception e) {
				logger.warn("Voucher_Id(20632) AVP value is error: "
						+ node.getStringValue() + ", " + e.toString());
			}
		}
		// Balance_Type
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Balance_Type");
		if (null != node) {
			try {
				rechargeRequest.setBalanceType(Integer.valueOf(node
						.getStringValue().trim()));
			} catch (Exception e) {
				logger.warn("Balance_Type(20655) AVP value is error: "
						+ node.getStringValue() + ", " + e.toString());
			}
		}
		// Destination_Balance_Unit
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Destination_Balance_Unit");
		if (null != node) {
			try {
				rechargeRequest.setRechargeUnit(Integer.valueOf(node
						.getStringValue().trim()));
			} catch (Exception e) {
				logger.warn("Destination_Balance_Unit(20637) AVP value is error: "
						+ node.getStringValue() + ", " + e.toString());
			}
		}
		// Voucher_Value
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Voucher_Value");
		if (null != node) {
			rechargeRequest.setRechargeAmount(Integer.valueOf(node
					.getStringValue()));
		}
		// Prolong_Days 20640
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Prolong_Days");
		if (null != node) {
			try {
				rechargeRequest.setProlongDays(Integer.valueOf(node
						.getStringValue().trim()));
			} catch (Exception e) {
				logger.warn("Prolong_Days(20640) AVP value is error: "
						+ node.getStringValue() + ", " + e.toString());
			}
		}
		// Destination_Type
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Destination_Type");
		if (null != node) {
			try {
				rechargeRequest.setObjType(Integer.valueOf(node
						.getStringValue().trim()));
			} catch (Exception e) {
				logger.warn("Destination_Type(20607) AVP value is error: "
						+ node.getStringValue() + ", " + e.toString());
			}
		}
		// Destination_Account
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Destination_Account");
		if (null != node) {
			rechargeRequest.setDestinationId(node.getStringValue());
		}
		// Destination_Attr
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Destination_Attr");
		if (null != node) {
			try {
				rechargeRequest.setDestinationAttr(Integer.valueOf(node
						.getStringValue().trim()));
			} catch (Exception e) {
				logger.warn("Destination_Attr(20647) AVP value is error: "
						+ node.getStringValue() + ", " + e.toString());
			}
		}
		// Destination_Attr_Detail
		node = requestElement
				.selectSingleNode("Service_Information/VC_Information/Destination_Attr_Detail");
		if (null != node) {
			try {
				rechargeRequest.setDestinationAttrDetail(Integer.valueOf(node
						.getStringValue().trim()));
			} catch (Exception e) {
				logger.warn("Destination_Attr_Detail(20667) AVP value is error: "
						+ node.getStringValue() + ", " + e.toString());
			}
		}
		// Area_Code
		// node = requestElement
		// .selectSingleNode("Service_Information/VC_Information/AreaCode");
		// if (null != node) {
		// try {
		// rechargeRequest.setAreaCode(node
		// .getStringValue().trim());
		// } catch (Exception e) {
		// logger.warn("AreaCode(20667) AVP value is error: "
		// + node.getStringValue() + ", " + e.toString());
		// }
		// }
		// Bonus_Info *
		List<org.dom4j.Node> bonus_nodes = requestElement
				.selectNodes("Service_Information/VC_Information/Bonus_Info");
		List<ocs.soap.Bonus> bonusList = new ArrayList<ocs.soap.Bonus>();
		ocs.soap.Bonus bonus = null;
		for (org.dom4j.Node bnode : bonus_nodes) {
			bonus = new ocs.soap.Bonus();
			// Bonus_Unit
			node = bnode.selectSingleNode("Bonus_Unit");
			if (null != node) {
				try {
					bonus.setBonusUnit(Integer.valueOf(node.getStringValue()));
				} catch (Exception e) {
					logger.warn("Bonus_Unit(20634) AVP value is error: "
							+ node.getStringValue() + ", " + e.toString());
				}
			}
			// Bonus_Amount
			node = bnode.selectSingleNode("Bonus_Amount");
			if (null != node) {
				try {
					bonus.setBonusAmount(Integer.valueOf(node.getStringValue()));
				} catch (Exception e) {
					logger.warn("Bonus_Amount(20620) AVP value is error: "
							+ node.getStringValue() + ", " + e.toString());
				}
			}
			// Balance_Type
			node = bnode.selectSingleNode("Balance_Type");
			if (null != node) {
				try {
					bonus.setBalanceType(Integer.valueOf(node.getStringValue()
							.trim()));
				} catch (Exception e) {
					logger.warn("Balance_Type(20655) AVP value is error: "
							+ node.getStringValue() + ", " + e.toString());
				}
			}
			// Bonus_Start_Time
			node = bnode.selectSingleNode("Bonus_Start_Time");
			if (null != node) {
				bonus.setStartTime(node.getStringValue());
			}
			// Bonus-Expiration-Time
			node = bnode.selectSingleNode("Bonus_Expiration_Time");
			if (null != node) {
				bonus.setEndTime(node.getStringValue());
			}
			bonusList.add(bonus);
		}
		if (!bonusList.isEmpty()) {
			rechargeRequest.setBonus(bonusList.toArray(new Bonus[bonusList
					.size()]));
			rechargeRequest.setRequestAmount(bonusList.size());
		}
		return rechargeRequest;
	}

	public static Element convertRechargeResponseToXML(RechargeResponse response) {
		Element serviceInformation = DocumentHelper
				.createElement("Service_Information");
		Element vcInformation = serviceInformation.addElement("VC_Information");

		Element element = null;
		element = vcInformation.addElement("Req_Serial");
		element.setText(response.getResponseId() == null ? "" : response
				.getResponseId());
		element = vcInformation.addElement("Result");
		element.setText(response.getResult() + "");
		// element.setText("4001" + "");
		Element balanceElement = vcInformation.addElement("Balance_Info");
		element = balanceElement.addElement("Destination_Balance");
		element.setText(response.getBalance() + "");
		element = balanceElement.addElement("Balance_Type");
		element.setText(response.getBalanceType() + "");
		element = balanceElement.addElement("Destination_Balance_Unit");
		element.setText(response.getBalanceUnit() + "");
		element = balanceElement.addElement("Destination_Effective_Time");
		element.setText(response.getEffectiveTime() == null ? "" : response
				.getEffectiveTime());
		element = balanceElement.addElement("Destination_Expiration_Time");
		element.setText(response.getExpirationTime() == null ? "" : response
				.getExpirationTime());
		logger.debug("convertRechargeResponseToXML:"
				+ serviceInformation.asXML());
		return serviceInformation;
	}

	public static AccountQueryRequest convertXMLToAccountQueryRequest(
			Element request) {
		AccountQueryRequest req = new AccountQueryRequest();
		org.dom4j.Node node = null;
		logger.debug("XML:" + request.asXML());
		// Req_Serial
		node = request
				.selectSingleNode("Service_Information/VC_Information/Req_Serial");
		if (null != node) {
			req.setRequestId(node.getStringValue());
		}
		// Destination_Type
		node = request
				.selectSingleNode("Service_Information/VC_Information/Destination_Type");
		if (null != node) {
			try {
				req.setObjType(Integer.valueOf(node.getStringValue()));
			} catch (Exception e) {
				logger.warn("Destination_Type(20607) AVP value is error :"
						+ node.getStringValue());
			}
		}
		// Destination_Account
		node = request
				.selectSingleNode("Service_Information/VC_Information/Destination_Account");
		if (null != node) {
			req.setDestinationId(node.getStringValue());
		}
		// Destination_Attr
		node = request
				.selectSingleNode("Service_Information/VC_Information/Destination_Attr");
		if (null != node) {
			try {
				req.setDestinationAttr(Integer.valueOf(node.getStringValue()));
			} catch (Exception e) {
				logger.warn("Destination_Attr(20647) AVP value is error :"
						+ node.getStringValue());
			}
		}
		// Balance_Type
		node = request
				.selectSingleNode("Service_Information/VC_Information/Balance_Type");
		if (null != node) {
			try {
				req.setBalanceType(Integer.valueOf(node.getStringValue()));
			} catch (Exception e) {
				logger.warn("Balance_Type(20655) AVP value is error :"
						+ node.getStringValue());
			}
		}
		return req;
	}

	public static Element convertAccountQueryResponseToXML(
			AccountQueryResponse response) {
		// {Req-Serial}
		// {Result}
		// *{Balance-Info}
		// {Destination-Balance}
		// {balance_type}
		// {Destination-Balance-Unit}
		// {Destination-Effective-Time}
		// {Destination-Expiration-Time}
		Element serviceInformation = DocumentHelper
				.createElement("Service_Information");
		Element vcInformation = serviceInformation.addElement("VC_Information");

		Element element = null;
		element = vcInformation.addElement("Result");
		element.setText(response.getResult() + "");

		element = vcInformation.addElement("Req_Serial");
		element.setText(response.getResponseId() == null ? "" : response
				.getResponseId());

		ocs.soap.Query[] querys = response.getQuery();
		for (ocs.soap.Query q : querys) {
			Element balanceInfo = vcInformation.addElement("Balance_Info");
			element = balanceInfo.addElement("Destination_Balance");
			element.setText(q.getBalance() + "");
			element = balanceInfo.addElement("Balance_Type");
			element.setText(q.getBalanceType() + "");
			element = balanceInfo.addElement("Destination_Balance_Unit");
			element.setText(q.getBalanceUnit() + "");
			element = balanceInfo.addElement("Destination_Effective_Time");
			element.setText(q.getEffectiveTime() + "");
			element = balanceInfo.addElement("Destination_Expiration_Time");
			element.setText(q.getExpirationTime() + "");
		}

		return serviceInformation;
	}

	public static RefundRequest convertXMLToRefundRequest(Element request) {

		RefundRequest req = new RefundRequest();
		org.dom4j.Node node = null;
		logger.debug("XML:" + request.asXML());
		// Req_Serial
		node = request
				.selectSingleNode("Service_Information/VC_Information/Req_Serial");
		if (null != node) {
			req.setRequestId(node.getStringValue());
		}
		// Resp_Serial

		node = request
				.selectSingleNode("Service_Information/VC_Information/Resp_Serial");
		if (null != node) {
			req.setOldRequestId(node.getStringValue());
		}
		// Destination_Type
		node = request
				.selectSingleNode("Service_Information/VC_Information/Destination_Type");
		if (null != node) {
			try {
				req.setObjType(Integer.valueOf(node.getStringValue()));
			} catch (Exception e) {
				logger.warn("Destination_Type(20607) AVP value is error :"
						+ node.getStringValue());
			}
		}

		// Destination_Account
		node = request
				.selectSingleNode("Service_Information/VC_Information/Destination_Account");
		if (null != node) {
			req.setDestinationId(node.getStringValue());
		}
		// Destination_Attr
		node = request
				.selectSingleNode("Service_Information/VC_Information/Destination_Attr");
		if (null != node) {
			try {
				req.setDestinationAttr(Integer.valueOf(node.getStringValue()));
			} catch (Exception e) {
				logger.warn("Destination_Attr(20647) AVP value is error :"
						+ node.getStringValue());
			}
		}
		return req;
	}

	public static Element convertRefundResponseToXML(RefundResponse response) {
		Element serviceInformation = DocumentHelper
				.createElement("Service_Information");
		Element vcInformation = serviceInformation.addElement("VC_Information");

		Element element = null;
		element = vcInformation.addElement("Result");
		element.setText(response.getResult() + "");

		element = vcInformation.addElement("Req_Serial");
		element.setText(response.getResponseId() == null ? "" : response
				.getResponseId());

		return serviceInformation;
	}

	public static TeleSaleAuthRequest convertXMLToTeleSaleAuthRequest(
			Element request) {
		TeleSaleAuthRequest ar = new TeleSaleAuthRequest();
		org.dom4j.Node node = request
				.selectSingleNode("Service_Information/VC_Information/Req_Serial");
		if (null != node) {
			ar.setRequestId(node.getStringValue());
		}
		node = request
				.selectSingleNode("Service_Information/VC_Information/Request_Time");
		if (null != node) {
			ar.setRequestTime(node.getStringValue());
		}
		node = request
				.selectSingleNode("Service_Information/VC_Information/Destination_Account");
		if (null != node) {
			ar.setObjId(node.getStringValue());
		}
		return ar;
	}

	public static Element convertUserAuthResponseToXML(
			TeleSaleAuthResponse response) {

		Element serviceInformation = DocumentHelper
				.createElement("Service_Information");
		Element vcInformation = serviceInformation.addElement("VC_Information");

		Element element = null;
		element = vcInformation.addElement("Resp_Serial");
		element.setText(response.getResponseId() == null ? "" : response
				.getResponseId());
		element = vcInformation.addElement("Return_Time");
		element.setText(response.getResponseTime() == null ? "" : response
				.getResponseTime());
		element = vcInformation.addElement("Result");
		element.setText(response.getResult() + "");

		return serviceInformation;
	}
}
